$(function(){
	unionBindCard();
	changeBankCode();
});
function changeBankCode(){
	$('.cardType').unbind('click');
	$('.cardType').bind('click',function(){
		var self = $(this);
		var val = self.val();
		$('#cardType').val(val);
	  });
	}
function unionBindCard(){
	$('#button').unbind('click');
	$('#button').bind('click',function(){
		var reg = /^(13[0-9]|14[57]|15[0-35-9]|17[0135678]|18[0-9])[0-9]{8}$/;
		var platformCuIdReg = /^[0-9]+$/;
		var merKeyReg = /^[A-Za-z0-9]+$/;
		var platformCuId = $('#platformCuId').val();
		var certIdReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
		var merKey = $('#merKey').val();
		var notifyUrl = $('#notifyUrl').val();
		var creditCard = $('#creditCard').val();
		var certId = $('#certId').val();
		var cardType = $('#cardType').val();
		var phone = $('#phone').val();
		if(!platformCuIdReg.test(platformCuId)){
			alertMsg('请输入正确的商户号!');
			return;
		}
		if (!reg.test(phone)) {
			alertMsg('请输入正确的手机号码!');
			return;
		}
		if(!platformCuIdReg.test(creditCard)){
			alertMsg('请输入正确的卡号!');
		}
		if(!certIdReg.test(certId)){
			alertMsg('请输入正确的身份证号!');
			return;
		}
		if(!merKeyReg.test(merKey)){
			alertMsg('请输入正确的merKey!');
			return;
		}
		if($.trim(notifyUrl) == ''){
			alertMsg('请输入正确的回调链接URL!');
			return;
		}
		var params = {
				platformCuId:platformCuId,
				notifyUrl:notifyUrl,
				phone:phone,
				creditCard:creditCard,
				certId:certId,
				cardType:cardType,
				merKey:merKey
		};
		
		var reqUrl = '../bind/unionBindCard';
		$.ajax({
			url: reqUrl,
			type: 'POST',
			//同步执行
			async:false,
			data: params,
			dataType: 'json',
			success: function(data) {
				var result = data.result;
				var result = eval('('+ result +')');
				if(result != undefined){
					var form = result.form;
					if(form != undefined){
						window.location.href = "${filter.union.bind.card.query.url}?form="+form;
						return;
					}else{
						$('#result').text(JSON.stringify(data));
					}
				}else{
					alertMsg("未知错误");
				}
			},
			error: function(data) {
				alertMsg("未知错误");
			}
		});
		
	});
}

function alertMsg(msg){
	var params = {
	    container : {
		    content :msg
		  },
		autoClose : 1500
	};
	easyDialog.open(params);
}